﻿using Clinic.Extensions;
using Clinic.IService;
using Clinic.Model.Dto;
using Clinic.View.Views;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Xml.Linq;

namespace Clinic.ViewModels.Main
{
    public partial class MenuViewModel : ObservableObject
    {
        private IUserService _userService;

        /// <summary>
        /// 权限
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<MenuNode> nodeTree;

        /// <summary>
        /// 菜单点击事件
        /// </summary>
        public AsyncRelayCommand<MenuNode> MenuSelectedCommand { get; }

        private List<MenuNode> _childrenMenu = new List<MenuNode>();

        public MenuViewModel(IUserService userService)
        {
            _userService = userService;
            nodeTree = new ObservableCollection<MenuNode>();
            MenuSelectedCommand = new AsyncRelayCommand<MenuNode>(MenuSelected);
            Events.OnTabItemChanged += tabItemChanged;
            var loadMenu = LoadMenu();
        }
        public async Task LoadMenu()
        {
            var list = await _userService.GetPermissionAsync(Guid.NewGuid());
            var parentMenu = list.Where(p => p.ParentMenuId == null).OrderBy(p => p.Sort);
            var childrenMenu = list.Where(p => p.ParentMenuId != null).OrderBy(p => p.Sort);
            NodeTree.Clear();
            _childrenMenu.Clear();
            foreach (var parent in parentMenu)
            {
                MenuNode parentNode = new MenuNode();
                parentNode.Id = parent.MenuId;
                parentNode.Name = parent.MenuName;
                parentNode.Icon = parent.MenuIcon;
                foreach (var child in childrenMenu.Where(p => p.ParentMenuId == parent.MenuId))
                {
                    MenuNode childNode = new MenuNode();
                    childNode.ViewName = child.ViewName;
                    childNode.Id = child.MenuId;
                    childNode.Name = child.MenuName;
                    childNode.Icon = child.MenuIcon;
                    parentNode.Childrens.Add(childNode);
                    _childrenMenu.Add(childNode);
                }
                NodeTree.Add(parentNode);
            }
        }

        public Task MenuSelected(MenuNode menuNode)
        {
            foreach (var item in _childrenMenu)
            {
                if (!item.Name.Equals(menuNode.Name))
                    item.IsSelected = false;
            }
            Events.OnContentChanged?.Invoke(menuNode.Name, Ioc.Default.GetView(menuNode.ViewName));
            return Task.CompletedTask;
        }

        private void tabItemChanged(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                _childrenMenu.ForEach((t) => t.IsSelected = false);
                return;
            }
            _childrenMenu.ForEach((t) =>
            {
                if (t.Name.Equals(name))
                    t.IsSelected = true;
                else
                    t.IsSelected = false;
            });
        }
    }

    public class MenuNode : ObservableObject
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string ViewName { get; set; }
        public string Icon { get; set; }
        public Guid? ParentId { get; set; }

        private bool _isSelected = false;
        /// <summary>
        /// 是否选中（子菜单）
        /// </summary>
        public bool IsSelected { get { return _isSelected; } set { _isSelected = value; OnPropertyChanged(); } }
        public ObservableCollection<MenuNode> Childrens { get; set; } = new ObservableCollection<MenuNode>();
    }
}
